package protocol;

import java.util.Random;
import stream.ByteCoding;

/**
 *
 * @author Simão Paulo Rato Alves Reis
 */
public abstract class ProtocolMessage {

    public long connectionid, messageid, userid;

    protected ProtocolMessage() {
        messageid = new Random().nextLong();
    }

    protected ProtocolMessage(byte[] stream) {
        connectionid = ByteCoding.setAsLong(8, stream);
        messageid = ByteCoding.setAsLong(16, stream);
        userid = ByteCoding.setAsLong(24, stream);
    }

    @Override
    public String toString() {
        return "\nConnection ID: " + connectionid
                + "\nMessage ID: " + messageid
                + "\nUser ID: " + userid;
    }

    public byte[] getBytes() {
        return ByteCoding.concat(
                ByteCoding.get(connectionid),
                ByteCoding.get(messageid),
                ByteCoding.get(userid));
    }

    public static int getTypeCode(byte[] stream) {
        return ByteCoding.setAsInt(0, stream);
    }
}
